<?php

namespace App\Http\Repositories\Daos\Impls;

use App\Http\Repositories\Daos\IBaseDao;
use App\Util\Helper;

/**
 * Created by PhpStorm.
 * User: Huyang
 * Date: 2016/9/12
 * Time: 16:59
 */
class BaseDao implements IBaseDao
{

    protected $model;

    public function __construct($model)
    {
        $this->model = $model;
    }

    public function find($id)
    {
        return $this->model->find($id);
    }

    public function byIds($ids, $key = 'id')
    {
        return $this->model->whereIn($key, $ids)->get();
    }

    public function byKey($key, $symbol, $val, $limit = 0)
    {
        $filterMap = $this->model->newQuery();
        $filterMap->where($key, '=', $val);
        if ($limit) {
            $filterMap->skip(0)->take($limit);
        }
        return $filterMap->orderBy('created_at','ASC')->get();
    }

    public function mapName($ids, $mapId = 'id', $mapField = 'name', $key = 'id') {
        $data = $this->model->whereIn($key, $ids)->get();
        if ($data) {
            $data = Helper::arrayMapByKey($data->toArray(), $mapId, $mapField);
        }
        return $data;
    }

    public function update($where, $data) {
        $filterMap = $this->model->newQuery();
        foreach ($where as $key => $record) {
            if (isset($record['symbol']) && isset($record['val'])) {
                if ($record['symbol'] != 'in') {
                    $filterMap->where($key, $record['symbol'], $record['val']);
                } else {
                    $filterMap->whereIn($key, $record['val']);
                }
            } else {
                $filterMap->where($key, '=', $record);
            }
        }
        return $filterMap->update($data);
    }

    public function count($where) {
        $filterMap = $this->model->newQuery();
        foreach ($where as $key => $record) {
            if (isset($record['symbol']) && isset($record['val'])) {
                if ($record['symbol'] != 'in') {
                    $filterMap->where($key, $record['symbol'], $record['val']);
                } else {
                    $filterMap->whereIn($key, $record['val']);
                }
            } else {
                $filterMap->where($key, '=', $record);
            }
        }
        return $filterMap->count();
    }

    public function exists($where)
    {
        $filterMap = $this->model->newQuery();
        foreach ($where as $key => $record) {
            if (isset($record['symbol']) && isset($record['val'])) {
                $filterMap->where($key, $record['symbol'], $record['val']);
            } else {
                $filterMap->where($key, '=', $record);
            }
        }
        return $filterMap->exists();
    }

    public function page($filterMap, $page, $size, $order = ['id'=>'DESC']) {
        $result = ['total'=>0, 'results'=>[]];
        $offset = intval(($page - 1) * $size);
        $size = intval($size) ? intval($size) : 10;
        $result['total'] = $filterMap->count();
        if ($result['total'] > 0) {
            if ((bool)$order) {
                foreach ($order as $key => $sort) {
                    $filterMap->orderBy($key, $sort);
                }
            }
            $result['results'] = $filterMap->skip($offset)->take($size)->get()->toArray();
        }
        return $result;
    }

    public function increment($field, $value = 1, $where = [], $update = []) {
        $filterMap = $this->model->newQuery();
        if (!empty($where)) {
            foreach ($where as $key => $record) {
                if (is_array($record) && isset($record['symbol']) && isset($record['val'])) {
                    $filterMap->where($key, $record['symbol'], $record['val']);
                } else {
                    $filterMap->where($key, '=', $record);
                }
            }
        }
        return $filterMap->increment($field, $value, $update);
    }

    public function decrement($field, $value, $where = [], $update = []) {
        $filterMap = $this->model->newQuery();
        if (!empty($where)) {
            foreach ($where as $key => $record) {
                if (is_array($record) && isset($record['symbol']) && isset($record['val'])) {
                    $filterMap->where($key, $record['symbol'], $record['val']);
                } else {
                    $filterMap->where($key, '=', $record);
                }
            }
        }
        return $filterMap->decrement($field, $value, $update);
    }

    public function toSql() {
        $filterMap = $this->model->newQuery();
        return $filterMap->getQueryLog();
    }

    public function whereInt($filterMap, $allow, $data, $isArray = true)
    {

    }

}